//define(['js/module.js'], function(controllers) {
	'use strict';
	angular.module('backcenter.orgSelectSingle', [])
	.controller('orgSelectSingleCtrl'
			,['$rootScope', '$scope','systemService', 'BizData'
			  , '$uibModalInstance','params', 'title'
			   , f])//控制器结束
		function f($rootScope, $scope, systemService, BizData
					   , $uibModalInstance, params, title
					   ) {
		$scope.headerText = '----------我是系统客户区orgSelectSingleCtrl控制器！id为-->'+$scope.$id+'开始----------';
		$scope.footerText = '----------我是系统客户区orgSelectSingleCtrl控制器！id为-->'+$scope.$id+'结束----------';

		var Model = this;
		
		

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataOrgBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
		}
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataOrgAfterDelete = function(event) {
//			alert('删除之后！')
		}

		/**
		name:bizData#onBeforeRefresh
		description: <b>[回调型事件]</b>业务数据刷新前
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"cancel" : 可修改，设置为true后中断当前刷新动作
		}
		</xmp>	
		*/
		Model.dataOrgBeforeRefresh = function(event){
			
		};
		
		
		/**
		name:bizData#onAfterRefresh
		description: <b>[回调型事件]</b>业务数据刷新后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象
			"limit" : 页大小, 
			"offset" : 偏移,
			"success" : 是否成功刷新
		}
		</xmp>	
		*/
		Model.dataOrgAfterRefresh = function(event){
			var dataTree = event.source;
			//展开第一个
			var rowID = dataTree.getCurrentID();
			if(rowID!=null){
				dataTree.expandRow(rowID);
			}
		}
		
		
		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
		"source" : 组件的js对象,
		"rowID" : 行Id,
		"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataOrgIndexChanged = function(event) {
		}
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
		"source" : 组件的js对象,
		"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
		"selfChanged" : 是否自身变化，false：从data触发的变化,
		"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataOrgDataChanged = function(event) {
		}
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
		"source" : 组件的js对象,
		"column" : 关系,
		"rowIndex" : 行索引,
		"value" : 新值,
		"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataOrgValueChanged = function(event) {
			
		}
		

		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		*/
		Model.dataOrgBeforeNew = function(event) {

		};
		
		
		/**
		* name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		* 
		* @param event
		*            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		*/
		Model.dataOrgAfterNew = function(event){

		}
		
		

		
		////////////////////////////////////////////////////////////////////////////////////////

		
		
		var options = {};
		options.container = Model;
		options.concept = 'SaOporg';
		options.name = 'dataOrg';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/backcenter/org/tree/create';
		options.reader = '/backcenter/org/tree/query';
		options.writer = '/backcenter/org/tree/save';
		options.deleter = '/backcenter/org/tree/delete';
		options.idColumnDefine = "sID";
		options.idColumnName = "sID";
		options.defCols = {sID:{name:"sID",type:"String",label:""},sName:{name:"sName",type:"String",label:""},sCode:{name:"sCode",type:"String",label:""},sLongName:{name:"sLongName",type:"String",label:""},sFName:{name:"sFName",type:"String",label:""},sFCode:{name:"sFCode",type:"String",label:""},sFID:{name:"sFID",type:"String",label:""},sOrgKindID:{name:"sOrgKindID",type:"String",label:""},sSequence:{name:"sSequence",type:"String",label:""},sValidState:{name:"sValidState",type:"Integer",label:""},sParent:{name:"sParent",type:"String",label:""},sNodeKind:{name:"sNodeKind",type:"String",label:""},sLevel:{name:"sLevel",type:"Integer",label:""},sPhone:{name:"sPhone",type:"String",label:""},sFax:{name:"sFax",type:"String",label:""},sAddress:{name:"sAddress",type:"String",label:""},sZip:{name:"sZip",type:"String",label:""},sDescription:{name:"sDescription",type:"String",label:""},sPersonID:{name:"sPersonID",type:"String",label:""},sCreatorFID:{name:"sCreatorFID",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = true;
		options.parentRelation = 'sParent';
		options.nodeKindRelation = 'sNodeKind';
		options.nodeLevelRelation = 'sLevel';
		options.nodeSortRelation = "sSequence";
		options.pathRelation = 'sFID';
		options.directDeleteMode = false;
   		options.limit = -1;
		options.orderBy = 'sSequence ASC';
		
		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataOrgIndexChanged;
		options.onBeforeNew = Model.dataOrgBeforeNew;
		options.onAfterNew = Model.dataOrgAfterNew;
		options.onBeforeDelete = Model.dataOrgBeforeDelete;
		options.onAfterDelete = Model.dataOrgAfterDelete;
		options.onValueChanged = Model.dataOrgValueChanged;
		options.onDataChanged = Model.dataOrgDataChanged;
//		options.rowsWatcherFn = Model.rowsWatcherFn;
		options.onAfterRefresh = Model.dataOrgAfterRefresh;
		
		$scope.dataOrg = new BizData(options);

		//监视对象中的所有变动
		$scope.dataOrg.deregRows = $scope.$watch('dataOrg.rows',$scope.dataOrg.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
    		Model.title = title;  
    		Model.params = params;  
    		
    		//增加过滤条件
    		$scope.dataOrg.filter = "SaOporg.sFID like '%"+Model.params.id+"%'";
//    		$scope.dataOrg.filter = "SaOporg.sCreatorFID = '" + Model.params.id + "'";
    		if(Model.params.psm==false){
    			$scope.dataOrg.filter = $scope.dataOrg.filter + " AND SaOporg.sOrgKindID<>'psm'"
    		}
    		if(Model.params.pos==false){
    			$scope.dataOrg.filter = $scope.dataOrg.filter + " AND SaOporg.sOrgKindID<>'pos'"
    		}
    		$scope.dataOrg.refreshData();

        });  
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	var sourceRow = $scope.dataOrg.rows[$scope.dataOrg.currentIndex];
	    	var row = {};
			//循环行中的列，判断变化点是哪一个
			for(var obj in sourceRow){
				if(obj!='$$hashKey' && obj!='userdata'){
					row[obj] = sourceRow[obj].value;
				}
			}
	    	
	    	data.push(row);
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    
	    
	    /*
	    //表格标题中的选择对话框ng-model
	    $scope.checkboxValue = false;
	    //表格标题中的选择对话框click事件
	    Model.checkboxClick = function () {  
			for(var index=0;index<$scope.dataOrg.rows.length;index++){
				$scope.dataOrg.rows[index].userdata.checked=$scope.checkboxValue;
			}
	    };    
	    */
		//////////////////////对话框使用结束//////////////////////////////////////


		
		
	    return Model;

	    
	    
	}	
	
//});
